package Lee_code;

import com.sun.org.apache.bcel.internal.generic.NEW;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * @author blh
 * @date 2024年07月10日 16:23
 */
public class Solution384 {
    int[] oriNums;
    List<List<Integer>> res = new ArrayList<>();

    public Solution384(int[] nums) {
        this.oriNums = nums;
        int len = nums.length;
        int[] seen = new int[len];
        backTrace(new ArrayList<>(), seen);
    }

    private void backTrace(ArrayList<Integer> list, int[] seen) {
        if (list.size() == oriNums.length) {
            res.add(new ArrayList<>(list));
            return;
        }
        for (int i = 0; i < oriNums.length; i++) {
            if (seen[i] != 1) {
                list.add(oriNums[i]);
                seen[i] = 1;
                backTrace(list, seen);

                // 重置
                list.remove(list.size()-1);
                seen[i] = 0;
            }
        }
    }


    public int[] reset() {
        return oriNums;
    }

    public int[] shuffle() {
        int len = oriNums.length;
        // 创建一个随机数生成器对象
        Random random = new Random();
        // 生成一个 0 到 10 之间的随机整数
        // 0到10，nextInt(n)生成的是[0, n)范围内的整数
        int index = random.nextInt(len);
        List<Integer> list = res.get(index);
        return list.stream().mapToInt(Integer::intValue).toArray();
    }
}
